package com.sc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sc.mapper.PostMapper;
import com.sc.mapper.UserMapper;
import com.sc.pojo.Post;
import com.sc.pojo.User;
import com.sc.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PostServiceImpl implements PostService {
    @Autowired
    PostMapper mapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public PageInfo<Post> show(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> list=mapper.show();
        PageInfo<Post> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public PageInfo<Post> mgrShow(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> list=mapper.mgrShow();
        PageInfo<Post> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public int add(Post p) {
        return mapper.insertSelective(p);
    }

    @Override
    public int del(Integer id) {
        //删除该帖子的评论
        mapper.delCommentsByPostId(id);
        //删除该帖子的点击记录
        mapper.delPostClickByPostId(id);
        //删除该帖子的点赞记录
        mapper.delPostSupportByPostId(id);
        //删除该帖子的评论记录
        mapper.delPostCommentByPostId(id);
        //删除该帖子
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delAll(Integer[] ids) {
        int i=0;
        for (Integer id:ids){
            //删除该帖子的评论
            mapper.delCommentsByPostId(id);
            //删除该帖子的点击记录
            mapper.delPostClickByPostId(id);
            //删除该帖子的点赞记录
            mapper.delPostSupportByPostId(id);
            //删除该帖子的评论记录
            mapper.delPostCommentByPostId(id);
            //删除该帖子
            i+=mapper.deleteByPrimaryKey(id);
        }
        return i;
    }

    @Override
    public int update(Post p) {
        return mapper.updateByPrimaryKeySelective(p);
    }

    @Override
    public PageInfo<Post> showMine(Integer writerid,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> list=mapper.showMine(writerid);
        PageInfo<Post> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public PageInfo<Post> supportPost(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> list=mapper.supportPost(userId);
        PageInfo<Post> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public PageInfo<Post> commentPost(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> list=mapper.commentPost(userId);
        PageInfo<Post> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public PageInfo<Post> clickPost(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> list=mapper.clickPost(userId);
        PageInfo<Post> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public PageInfo<Post> search(Integer pageNum, Integer pageSize, Post post) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> list=mapper.search(post);
        PageInfo<Post> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public PageInfo<Post> mgrSearch(Integer pageNum, Integer pageSize, Post post) {
        PageHelper.startPage(pageNum,pageSize);
        List<Post> list=mapper.mgrSearch(post);
        PageInfo<Post> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public int addClick(Integer id) {
        return mapper.addClick(id);
    }

    @Override
    public int addUserClick(Integer postid, Integer clickid) {
        return mapper.addUserClick(postid,clickid);
    }

    @Override
    public Post lookPost(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    //查出点赞该帖子的人
    @Override
    public List<Integer> queryGoodId(Integer id) {
        return mapper.queryGoodId(id);
    }

    @Override
    public int addSupport(Integer id) {
        return mapper.addSupport(id);
    }

    @Override
    public int addUserSupport(Integer postid, Integer supportid) {
        return mapper.addUserSupport(postid,supportid);
    }

    @Override
    public int cancelSupport(Integer id) {
        return mapper.cancelSupport(id);
    }

    @Override
    public int delUserSupport(Integer postid, Integer supportid) {
        return mapper.delUserSupport(postid,supportid);
    }

    @Override
    public User queryHomeUser(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Post> homePost(Integer writerid) {
        return mapper.showHome(writerid);
    }

    @Override
    public int homeSupports(Integer id) {
        return mapper.homeSupports(id);
    }

    @Override
    public int top(Integer id) {
        return mapper.top(id);
    }

    @Override
    public int cancelTop(Integer id) {
        return mapper.cancelTop(id);
    }

    @Override
    public int good(Integer id) {
        return mapper.good(id);
    }

    @Override
    public int cancelGood(Integer id) {
        return mapper.cancelGood(id);
    }

    @Override
    public int ban(Integer id) {
        return mapper.ban(id);
    }

    @Override
    public int cancelBan(Integer id) {
        return mapper.cancelBan(id);
    }
}
